import Index from "../views/Index.vue"
import Home from "../views/Home.vue"
import HomeIndex from "../views/home/HomeIndex.vue"
import PublishArticle from "../views/article/PublishArticle.vue"
import ArticleDetails from "../views/article/ArticleDetails.vue"
import StringUtil from '../utils/StringUtil'
import UserInfoSetting from '../views/userInfo/UserInfoSetting'
import UserInfo from '../components/userInfoSetting/UserInfo.vue'
import UserSecurity from '../components/userInfoSetting/UserSecurity.vue'
import UserTheme from '../components/userInfoSetting/UserTheme.vue'
import UserConfiguration from '../components/userInfoSetting/UserConfiguration.vue'
import { createRouter, createWebHashHistory } from 'vue-router'

const router = new createRouter({
    routes: [
        {
            path: "/login",
            component: Index
        },
        {
            path: "/",
            component: Home,
            children: [
                {
                    path: "home",
                    component: HomeIndex
                },
                {
                    path: "publish-article",
                    component: PublishArticle
                },
                {
                    name: "article-details",
                    path: "article-details/:articleId",
                    component: ArticleDetails
                },
                {
                    path: 'user-info-setting',
                    name: 'user-info-setting',
                    component: UserInfoSetting,
                    children: [
                        {
                            path: 'user-info',
                            name: 'user-info',
                            component:UserInfo
                        },
                        {
                            path: 'user-security',
                            name: 'user-security',
                            component:UserSecurity
                        },
                        {
                            path: 'user-theme',
                            name: 'user-theme',
                            component:UserTheme
                        },
                        {
                            path: 'user-configuration',
                            name: 'user-configuration',
                            component:UserConfiguration
                        }
                    ]
                }
            ]
        }

    ],
    history: createWebHashHistory()
})

//全局前置守卫,没有登录或登录过期后跳转到登录页
router.beforeEach((to, from, next) => {
    //放行登录页面
    if (to.fullPath === "/login") {
        next();
    }
    let token = localStorage.getItem("token");
    if (StringUtil.isEmpty(token)) {
        next("/login");
    }
    if (to.fullPath === "/") {
        next("/home");
    }

    next();
})


export default router